//按钮点击事件
import { base, ajax, toast, print, output, printer, getBusinessInfo, cardCache, promptBox } from 'nc-lightapp-front';
import linkBudgetApi from '../../../../../public/components/linkbudget/linkbudget';
import linkVoucherApi from '../../../../../public/components/linkvoucher/linkvoucher';
import config from '../config';
import api from '../../../common/api';

const { setDefData } = cardCache;

export default function (props, key) {
    const status = props.getUrlParam('status');
    props.cardTable.filterEmptyRows(config.bodycode, 'assume_amount');
    let reqData = {};
    switch (key) {
        case 'refresh':
            this.loadData();
            break;
        case 'save':
            this.save();
            break;

        case 'temp_save':
            this.saveTemporarily();
            break;

        case 'edit':
            this.setPageStatus('edit');
            break;

        case 'cancel':
            if (status === 'add') {
                props.pushTo('/list', {
                    status: 'browse'
                });
            } else if (status === 'edit') {
                this.setPageStatus('browse');
                this.loadData();
            } else {
                props.pushTo('/list', {
                    status: 'browse'
                });
            }
            break;

        case 'delete':
            this.delete();
            break;

        case 'approve':
            this.approve();
            break;

        case 'unapprove':
            this.cancelApprove();
            break;

        case 'link_bx':
            api.getReimbursementByLink({
                billtypeCode: '264X',
                billid: props.form.getFormItemsValue(config.headcode, 'src_id').value,
                transtypeCode: props.form.getFormItemsValue(config.headcode, 'djlxbm').value
            }).then((data) => {
                props.specialOpenTo(data.url, {
                    pagecode: data.data.pagecode,
                    appcode: data.data.appcode,
                    scene: 'lc',
                    status: 'browse',
                    id: data.data.id,
                    tradetype: data.data.tradetype
                });
            });
            break;

        case 'link_voucher':
            linkVoucherApi.link({
                data: {
                    props: props,
                    record: {
                        relationID: props.form.getFormItemsValue(config.headcode, 'pk_costshare').value,
                        pk_billtype: props.form.getFormItemsValue(config.headcode, 'pk_tradetype').value
                    },
                    appid: props.getSearchParam('ar')
                }
            });
            break;

        case 'link_budget':
            linkBudgetApi.link({
                data: {
                    tradetype: props.form.getFormItemsValue(config.headcode, 'pk_tradetype').value,
                    openbillid: props.form.getFormItemsValue(config.headcode, 'pk_costshare').value
                },
                success: (data) => {
                    this.setState({
                        inspectionSourceData: data,
                        isInspectionShowed: true
                    });
                }
            });
            break;

        // 快速分摊
        case 'quickShare': {
            const total = Number(props.form.getFormItemsValue(config.headcode, 'total').value);
            if(total <= 0) {
                toast({content: this.state.multilang['2207KSFT-0002'], color:'warning'});
                return;
            }
            // Todo 如果是整单分摊需要提示清空，如果是继续分摊直接进入分摊规则
            const index_obj = props.cardTable.getClickRowIndex(config.bodycode);
            //未选择明细行
            if(!index_obj || (index_obj.record && index_obj.record.status == '3' )) {
                const rows = props.cardTable.getVisibleRows(config.bodycode);
                //表体为空直接整单分摊
                if(rows.length == 0) {
                    this.setState({showQuickShare: true});
                    return;
                }
                // 整单分摊提示
                promptBox({
                    color: 'warning',               // 提示类别默认"success"， "success"/"info"/"warning"/"danger",非必输
                    title: "警告",
                    content: this.state.multilang['2207KSFT-0004'],
                    beSureBtnClick: () => {
                        this.setState({showQuickShare: true})
                    },
                    cancelBtnClick: () => {

                    }
                })
            //选择明细行
            } else {
                //没有承担金额提示
                let assume_amount = index_obj.record.values.assume_amount;
                if(!assume_amount || !assume_amount.value || assume_amount.value<= 0) {
                    toast({ content: this.state.multilang['2207KSFT-0003'], color: 'warning' });
                    return;
                }
                // 有选中分摊
                this.setState({showQuickShare: true})
                // setTimeout(() => {
                //     // 取消选中
                //     props.cardTable.unSelectRowsByIndex(config.headcode, index_obj.index)
                // }, 1000)
            }
				break;
        }
        default:
            break;
    }
}
